package init;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class InitApp extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public InitApp() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
		String dburl=getInitParameter("dburl");
		String username=getInitParameter("username");
		String password=getInitParameter("password");
//		String appname=getInitParameter("appname");
//		String apphost=getInitParameter("apphost");
//		String appport=getInitParameter("appport");
//		String caption=getInitParameter("caption");
//		String cookiename=getInitParameter("cookiename");
		Connection con=null;
		/**
		 * config表必须对应：
		 * id=1,name=appname;
		 * id=2,name=apphost;
		 * id=3,name=appport;
		 * id=4,name=cookiename;
		 * id=5,name=caption
		 */
		try {
			Class.forName("com.mysql.jdbc.Driver");
			con=DriverManager.getConnection(dburl,username,password);
			Statement sm=con.createStatement();
			String sqls1="SELECT value FROM config";
			ResultSet rs=sm.executeQuery(sqls1);
			rs.last();
			if(rs.getRow()!=5){
				log.log.err("出现致命初始化错误，本系统必须停止运行！");
				System.exit(1);
			}
			rs.beforeFirst();
			rs.next();
			String appname=rs.getString(1);
			rs.next();
			String apphost=rs.getString(1);
			rs.next();
			String appport=rs.getString(1);
			rs.next();
			String cookiename=rs.getString(1);
			rs.next();
			String caption=rs.getString(1);
			ServletContext application=getServletContext();
			application.setAttribute("con", con);
			application.setAttribute("appname", appname);
			application.setAttribute("apphost", apphost);
			application.setAttribute("appport", appport);
			application.setAttribute("caption", caption);
			application.setAttribute("cookiename", cookiename);
			String link="http://"+apphost+":"+appport+"/"+appname+"/";
			application.setAttribute("homelink", link);
			log.log.log("数据库连接初始化完成！");
			log.log.log("数据点：");
			log.log.log("appname="+appname);
			log.log.log("apphost="+apphost);
			log.log.log("appport="+appport);
			log.log.log("cookiename="+cookiename);
			log.log.log("caption="+caption);
			log.log.log("在线考试系统OnlineTest现在开始运行！");
		} catch (Exception e) {
			log.log.err(e.toString());
			log.log.err("出现致命初始化错误，本系统必须停止运行！");
			System.exit(1);
		}
	}

}
